Skip to content

Conversation

@alien-0119
Copy link
Collaborator

@alien-0119 alien-0119 commented Oct 27, 2025

What does this PR do?

Adds # (feature)
Add hgnet_v2 model and fast ut.

Usage Example:

import mindspore as ms
import requests
from mindone.transformers import HGNetV2ForImageClassification
from transformers import AutoImageProcessor
from PIL import Image


url = "http://images.cocodataset.org/val2017/000000039769.jpg"
image = Image.open(requests.get(url, stream=True).raw)

model = HGNetV2ForImageClassification.from_pretrained("ustc-community/hgnet-v2")
processor = AutoImageProcessor.from_pretrained("ustc-community/hgnet-v2")

inputs = processor(images=image, return_tensors="np")
inputs = {k: ms.tensor(v).to(dtype) for k, v in inputs.items()}

logits = model(**inputs).logits
predicted_class_id = logits.argmax(dim=-1).item()

class_labels = model.config.id2label
predicted_class_label = class_labels[predicted_class_id]
print(f"The predicted class label is: {predicted_class_label}")
# LABEL_0

Performance:
Experiments were tested on Ascend Atlas 800T A2 machines with mindspore 2.7.0 pynative mode.

  • mindspore.nn.MaxPool2d does not support bf16 inputs
model precision weight load(s) s/step
ustc-community/hgnet-v2 fp32 21.629 0.026
ustc-community/hgnet-v2 fp16 23.190 0.024

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline?
  • Did you make sure to update the documentation with your changes? E.g. record bug fixes or new features in What's New. Here are the
    documentation guidelines
  • Did you build and run the code without any errors?
  • Did you report the running environment (NPU type/MS version) and performance in the doc? (better record it for data loading, model inference, or training tasks)
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@xxx

@alien-0119 alien-0119 requested a review from vigo999 as a code owner October 27, 2025 08:39
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @alien-0119, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces the HGNetV2 model to the mindone/transformers framework, expanding the library's capabilities in computer vision. The changes encompass the full implementation of the HGNetV2 architecture, its pre-trained model, and a specific variant for image classification. This integration ensures that users can leverage HGNetV2 through the standard transformers API, with robust testing to validate its performance and compatibility within the MindSpore ecosystem.

Highlights

  • New Model Integration: The HGNetV2 model, including its backbone and image classification capabilities, has been added to the mindone/transformers library.
  • Auto-Configuration Support: The new HGNetV2 model is fully integrated into the auto-configuration and auto-modeling systems, allowing for easy instantiation and usage within the framework.
  • Comprehensive Testing: Dedicated tests have been introduced for the HGNetV2 model to ensure functional correctness and precision consistency between PyTorch and MindSpore implementations.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds the HGNet-V2 model, including its backbone and image classification head, along with corresponding tests. The implementation is well-structured and integrates with the existing auto-model framework. My review includes a few suggestions for improvement: renaming a parameter that shadows a Python built-in, refactoring a part of the classification head to use more idiomatic MindSpore code, correcting a typo in a test filename, and, most importantly, extending the tests to cover graph mode, which is crucial for ensuring model correctness in MindSpore.


# ms.nn.MaxPool2d does not support bf16 inputs
DTYPE_AND_THRESHOLDS = {"fp32": 6e-4, "fp16": 5e-3}
MODES = [1]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The tests are currently only running in PyNative mode (ms.PYNATIVE_MODE which is 1). It's important to also test in Graph mode (ms.GRAPH_MODE which is 0) to ensure the model can be correctly compiled and executed. Please consider adding it to the MODES list.

Suggested change
MODES = [1]
MODES = [0, 1]

else:
self.lab = mint.nn.Identity()

def construct(self, input: Tensor) -> Tensor:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The parameter input shadows a built-in Python function. It's a good practice to avoid this. I suggest renaming it to hidden_state for clarity and consistency with the variable's usage within the method.

Suggested change
def construct(self, input: Tensor) -> Tensor:
def construct(self, hidden_state: Tensor) -> Tensor:

)

# classification head
self.classifier = nn.CellList([self.avg_pool, self.flatten])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using nn.CellList with a for loop in the construct method for a simple sequence of layers is less idiomatic than using nn.SequentialCell. I recommend refactoring this to nn.SequentialCell.

self.classifier = nn.SequentialCell(self.avg_pool, self.flatten)

Then, in the construct method (lines 447-449), you can replace the loop over self.classifier with a single call:

pooled_output = self.classifier(last_hidden_state)
logits = self.fc(pooled_output)

This will make the code cleaner and more aligned with common MindSpore practices.

@@ -0,0 +1,223 @@
"""Adapted from https://github.com/huggingface/transformers/tree/main/tests/models/hgnet_v2/test_modeling_hgnet_v2.py."""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

There seems to be a typo in the filename. It should probably be test_modeling_hgnet_v2.py instead of test_modeing_hgnet_v2.py.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant